﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;


namespace asscope.core.Services
{
    public class Title
    {


        private static core.Entities.Title CreateEmptyEntities()
        {
            var entity = new core.Entities.Title
            {
                id = string.Empty,
                code = string.Empty,
                name = string.Empty
            };
            return entity;
        }



        public static core.Entities.Title ConvertToEntity(DataRow dr)
        {
            var entity = new Entities.Title
            {
                id = (dr["id"] != DBNull.Value) ? dr["id"].ToString() : string.Empty,
                code = (dr["code"] != DBNull.Value) ? dr["code"].ToString() : string.Empty,
                name = (dr["name"] != DBNull.Value) ? dr["name"].ToString() : string.Empty
            };
            return entity;
        }



        public Entities.Title[] SelectAll()
        {
            var cmd = new SqlCommand
            {
                CommandType = CommandType.Text,
                CommandText = "select * from titles",
                CommandTimeout = 0
            };

            var dt = SQLHelper.ExecuteDataset(cmd).Tables[0];



            if (dt.Rows.Count > 0)
            {
                var entities = new Entities.Title[dt.Rows.Count];
                var i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    entities[i] = ConvertToEntity(dr);
                    i++;
                }
                return entities;
            }
            var entitie = new Entities.Title[1];
            entitie[0] = CreateEmptyEntities();
            return entitie;
        }

    }
}
